﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VINETLIFE.BL;
using VINETLIFE.DO;

using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.XSSF.UserModel;
using System.Data;
using NPOI.SS.UserModel;
using System.IO;

namespace VINETLIFE.AdminCP
{
    public partial class OrderStatictis : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlCustomerDataSource();
                grvLogAccountDataSource();
            }
        }

        protected void ddlCustomerDataSource()
        {
            ddlCustomer.DataSource = new cmsCustomerBL().GetByIsVangLai(false);
            ddlCustomer.DataBind();
        }
        protected void grvLogAccount_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvLogAccount.PageIndex = e.NewPageIndex;
            grvLogAccountDataSource();
        }
        protected void grvLogAccountDataSource()
        {

            grvLogAccount.DataSource = dtSource();
            grvLogAccount.DataBind();
            // }
        }

        public DataTable dtSource()
        {
            int customerID = int.Parse(ddlCustomer.SelectedValue);
            //  cmsAccountDO objAccount = new cmsAccountBL().AccountInfoObject(customerID);
            //if (objAccount != null)
            //{
            string type = "";
            DateTime searchDate = new DateTime(1900, 1, 1);
            DateTime fromDate = new DateTime(1900, 1, 1);
            DateTime toDate = new DateTime(1900, 1, 1);

            if (rdSearchDate.Checked)
            {
                type = "date";
                if (txtSearchDate.Text != "")
                {
                    searchDate = Myfunction.ReturnValidateDateTime(txtSearchDate.Text.Trim());
                }
                else
                {
                    type = "";
                }
            }
            if (rdFromDate.Checked)
            {
                type = "fromdate";
                if (txtFromDate.Text != "")
                {
                    fromDate = Myfunction.ReturnValidateDateTime(txtFromDate.Text.Trim());
                }
                if (txtToDate.Text != "")
                {
                    toDate = Myfunction.ReturnValidateDateTime(txtToDate.Text.Trim());
                }
                if (txtFromDate.Text.Equals("") || txtToDate.Text.Equals(""))
                {
                    type = "";
                }
            }
            return new cmsAccountReceiveMoneyBL().Search(type, searchDate, fromDate, toDate, "", customerID);
        }
        public string SetStatus(object e)
        {
            if (e == null)
            {
                return "N/A";
            }
            else
            {
                switch (e.ToString())
                {
                    case "PROCESSING":
                        return "Đang xử lý";
                        break;
                    case "OK":
                        return "Thực nhận";
                        break;
                    case "RECEIVED":
                        return "Đã nhận tiền";
                        break;
                    case "CANCEL":
                        return "Đã hủy đơn hàng";
                        break;
                    default:
                        return "";
                        break;
                }
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (rdbGeneral.Checked)
            {
                DataTableToExcel(dtSource(), "Bao_cao_tong_quat_" + DateTime.Now.ToString("dd_MM_yyyy"));
            }
            else if (rdbDetail.Checked)
            {
                DataTableToExcel(dtSource(), "Bao_cao_chi_tiet_" + DateTime.Now.ToString("dd_MM_yyyy"));
            }
        }


        public static void DataTableToExcel(DataTable dt, String fileName)
        {

            //Make a new npoi workbook

            HSSFWorkbook hssfworkbook = new HSSFWorkbook();



            //Here I am making sure that I am giving the file name the right

            //extension:

            string filename = "";

            if (fileName.EndsWith(".xls"))

                filename = fileName;

            else

                filename = fileName + ".xls";



            //This starts the dialogue box that allows the user to download the file



            System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;



            Response.ContentType = "application/vnd.ms-excel";

            Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename));

            Response.Clear();



            //make a new sheet – name it any excel-compliant string you want

            //HSSFSheet sheet1 = hssfworkbook.CreateSheet("Sheet 1″);

            ISheet sheet1 = hssfworkbook.CreateSheet("Sheet 1");

            sheet1.SetColumnWidth(0, 50 * 100);
            sheet1.SetColumnWidth(1, 50 * 100);
            sheet1.SetColumnWidth(2, 50 * 100);
            sheet1.SetColumnWidth(3, 50 * 100);
            sheet1.SetColumnWidth(4, 50 * 100);
            //make a header row

            IRow row1 = sheet1.CreateRow(0);

            //Puts in headers (these are table row headers, omit if you

            //just need a straight data dump

            DataTable dtTemp = dt;

            DataTable dtSchema = new DataTable();
            dtSchema.Columns.Add("Mã đơn hàng", typeof(String));
            dtSchema.Columns.Add("Ngày mua", typeof(DateTime));
            dtSchema.Columns.Add("Giá trị đơn hàng", typeof(Int64));
            dtSchema.Columns.Add("Họ tên", typeof(String));
            dtSchema.Columns.Add("Mã số Vinetlife", typeof(String));

            foreach (DataRow dr in dtTemp.Rows)
            {
                DataRow drNew = dtSchema.NewRow();
                drNew["Mã đơn hàng"] = dr["OrderCode"];
                drNew["Ngày mua"] = dr["CreatedDate"];
                drNew["Giá trị đơn hàng"] = dr["ReceivedValue"];
                drNew["Họ tên"] = dr["FullName"];
                drNew["Mã số Vinetlife"] = dr["CustomerCode"];
                dtSchema.Rows.Add(drNew);
            }

            for (int j = 0; j < dtSchema.Columns.Count; j++)
            {

                ICell cell = row1.CreateCell(j);
                String columnName = dtSchema.Columns[j].ToString();

                cell.SetCellValue(columnName);
            }
            //loops through data

            for (int i = 0; i < dtSchema.Rows.Count; i++)
            {

                IRow row = sheet1.CreateRow(i + 1);

                for (int j = 0; j < dtSchema.Columns.Count; j++)
                {

                    ICell cell = row.CreateCell(j);

                    String columnName = dtSchema.Columns[j].ToString();

                    cell.SetCellValue(dtSchema.Rows[i][columnName].ToString());



                }

            }

            //writing the data to binary from memory

            Response.BinaryWrite(WriteToStream(hssfworkbook).GetBuffer());

            Response.End();



        }

        static MemoryStream WriteToStream(HSSFWorkbook hssfworkbook)
        {

            //Write the stream data of workbook to the root directory

            MemoryStream file = new MemoryStream();

            hssfworkbook.Write(file);

            return file;

        }
    }
}